package com.ruoyi.business.controller;

import com.ruoyi.business.domain.DisplayVideo;
import com.ruoyi.business.dto.VideoListDTO;
import com.ruoyi.business.service.IDisplayVideoService;
import com.ruoyi.business.vo.VideoInfoVO;
import com.ruoyi.business.vo.VideoListVO;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.file.FileUploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.Video;
import com.ruoyi.business.service.IVideoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 视频Controller
 * 
 * @author lcz
 * @date 2025-01-19
 */
@Api(tags = "视频相关接口")
@RestController
@RequestMapping("/business/video")
public class VideoController extends BaseController
{
    @Autowired
    private IVideoService videoService;

    /**
     * 查询视频列表
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:video:list')")
    @GetMapping("/list")
    public TableDataInfo list(Video video)
    {
        startPage();
        List<Video> list = videoService.selectVideoList(video);
        return getDataTable(list);
    }

    /**
     * 导出视频列表
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:video:export')")
    @Log(title = "视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Video video)
    {
        List<Video> list = videoService.selectVideoList(video);
        ExcelUtil<Video> util = new ExcelUtil<Video>(Video.class);
        util.exportExcel(response, list, "视频数据");
    }

    /**
     * 获取视频详细信息
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:video:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(videoService.selectVideoById(id));
    }

    /**
     * 新增视频
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:video:add')")
    @Log(title = "视频", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Video video)
    {
        return toAjax(videoService.insertVideo(video));
    }

    /**
     * 修改视频
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:video:edit')")
    @Log(title = "视频", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Video video)
    {
        return toAjax(videoService.updateVideo(video));
    }

    /**
     * 删除视频
     */
    @ApiIgnore
    @PreAuthorize("@ss.hasPermi('business:video:remove')")
    @Log(title = "视频", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(videoService.deleteVideoByIds(ids));
    }

    /** 前端接口 ***************************************************************************************************/
    /** 前端接口 ***************************************************************************************************/
    /** 前端接口 ***************************************************************************************************/

    @Autowired
    private IDisplayVideoService displayVideoService;

    /**
     * 查询视频列表
     * 接口路径：/video/front/list
     */
    @GetMapping("/front/list")
    @ApiOperation("查询视频列表")
    public TableDataInfo<VideoListVO> frontList(VideoListDTO dto)
    {
        startPage();
        List<VideoListVO> list = videoService.selectVideoListVO(dto);
        DisplayVideo displayVideo = displayVideoService.selectDisplayVideoById(1l);
        Boolean isDisplayVideo = displayVideo.getIsDisplayVideo()==1?true:false;
        TableDataInfo res = getDataTable(list);
        res.setIsDisplayVideo(isDisplayVideo);
        return res;
    }

    /**
     * 获取视频详细信息
     * 接口路径：/video/front/info
     */
    @ApiOperation("获取视频详情")
    @GetMapping(value = "/front/info")
    public R<VideoInfoVO> frontInfo(@RequestParam("id") Long id)
    {
        VideoInfoVO video = videoService.frontInfo(id);
        return R.ok(video);
    }

    @GetMapping("/changePath")
    public R changePath(){
        List<Video> list = videoService.selectVideoList(new Video());
        for (Video video : list) {
            String link = video.getLink();
            String[] temp = link.split("/");
            String[] stringList = temp[temp.length-1].split("_");
            if (FileUploadUtils.isContainsChinese(stringList[0])){
                // 将stringList中除了第一个元素之外的所有元素按顺序进行拼接，中间用"_"分隔
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < temp.length-1; i++){
                    sb.append(temp[i]);
                    sb.append("/");
                }
                for (int i = 1; i < stringList.length; i++) {
                    sb.append(stringList[i]);
                    if (i < stringList.length - 1) {
                        sb.append("_");
                    }
                }
                video.setLink(String.valueOf(sb));
                videoService.updateVideo(video);
            }
        }
        return R.ok();
    }

//    /**
//     * 新增视频
//     * 接口路径：/video/front/add
//     */
//    @ApiOperation("前台新增视频")
//    @PostMapping("/front/add")
//    public AjaxResult frontAdd(@RequestBody Video video)
//    {
//        return toAjax(videoService.insertVideo(video));
//    }
//
//    /**
//     * 修改视频
//     * 接口路径：/video/front/edit
//     */
//    @ApiOperation("前台修改视频")
//    @PostMapping("/front/edit")
//    public AjaxResult frontEdit(@RequestBody Video video)
//    {
//        return toAjax(videoService.updateVideo(video));
//    }
//
//    /**
//     * 删除视频
//     * 接口路径：/video/front/delete
//     */
//    @ApiOperation("前台删除视频")
//    @GetMapping("/front/delete")
//    public AjaxResult frontRemove(@RequestParam Long[] ids)
//    {
//        return toAjax(videoService.deleteVideoByIds(ids));
//    }
}
